package com.hfi;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ChangLiang
 * @date 2019/5/21
 */
public class App 
{
    public void generator() throws Exception {

        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        //指定 逆向工程配置文件

/*		String path = App.class.getResource("/").getPath();
		System.out.println(path)*/;

        String path = Thread.currentThread().getContextClassLoader().getResource("generatorConfig.xml").getPath();
        System.out.println(path);

        /*String path = new File("").getAbsoluteFile() + "\\mybatisGeneratorSqlmapper\\generatorConfig.xml";
        System.out.println(path);*/

        FileInputStream fileInputStream = new FileInputStream(new File(path));

        // generatorConfig.xml位于项目根路径下 所以直接new File就可以了
//        FileInputStream fileInputStream = new FileInputStream(new File("generatorConfig.xml"));

        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(fileInputStream);

        /*File configFile = new File("generatorConfig.xml");
        System.out.println(configFile.exists());
        System.out.println(new File(".").getAbsoluteFile());
        System.out.println(new File("").getAbsoluteFile()+"\\mybatisGeneratorSqlmapper\\generatorConfig.xml");*/


/*        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);*/

        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
                callback, warnings);
        myBatisGenerator.generate(null);

    }

    public static void main(String[] args) {
        try {
            App generatorSqlmap = new App();
            generatorSqlmap.generator();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
